package com.apps.ubc.cc;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.http.*;

import com.apps.datastore.NoSignupDatastore;
import com.apps.datastore.dao.UniqueCourseObject;
import com.apps.outgoing.EmailNotifier;
import com.apps.services.UBCSectionDetailService;
import com.apps.utils.EmailUtils;

public class RegisterNoSignUpServlet extends HttpServlet {
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		new NoSignupDatastore();
		String email = req.getParameter("email");
		String phone = req.getParameter("phone");
		String dept = req.getParameter("dept").toUpperCase();
		String course = req.getParameter("course").toUpperCase();
		String section = req.getParameter("section").toUpperCase();
		
		UBCSectionDetailService wrapper = new UBCSectionDetailService();
		int code = wrapper.init(dept, course, section);
		if(code != 1)
			resp.sendRedirect("/error404.jsp");
		
		String key = NoSignupDatastore.addNotifier(email, phone, dept, course, section);
		String courseFormatted = dept + " " + course + " " + section;
		sendVerification(email, key, courseFormatted);
		resp.sendRedirect("/nsconfirm.jsp?email="+URLEncoder.encode(email,"UTF-8"));

	}
	
	private void sendVerification(String email, String key, String course){
		String body = EmailUtils.getNSVerify(key, course);
		EmailNotifier.sendMessage(email, body);
	}


}
